package cp7_jstl_javabean.user;

import bean.UserInfo;
import util.DBTool;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

// 完成修改 1。查询并显示要修改的数据 2.修改数据
@WebServlet("/cp7/user/modify")
public class ModifyServlet extends HttpServlet {
    // 查询并显示要修改的数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String sql = "select * from userinfo where id=" + id;
        try {
            ResultSet rs = DBTool.executeQuery(sql); //
            // 创建一个user对象
            UserInfo u = null;
            if (rs.next()) {
                u = new UserInfo();
                // 读取结果
                u.setId(rs.getInt("id"));
                u.setNickname(rs.getString("nickname"));
                u.setStatus(rs.getInt("status"));
                u.setAccount(rs.getString("account"));
                u.setEmail(rs.getString("email"));
                u.setCreateTime(rs.getDate("create_time"));
                u.setHeaderImage(rs.getString("header_image"));
                u.setHeaderUrl(rs.getString("header_url"));
                u.setPassword(rs.getString("password"));
                u.setUpdateTIme(rs.getDate("update_time"));
            }
            Statement st = rs.getStatement();
            Connection conn = st.getConnection();
            DBTool.closeResultSet(rs);
            DBTool.closeStatement(st);
            DBTool.closeConnection(conn);
            if (u == null) {
                StringUtil.getWriter(resp).println("没有查询到要修改的数据");
            } else {
                System.out.println(u);
                req.setAttribute("user", u);
                req.getRequestDispatcher("/cp7/modify.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 修改数据
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置读取数据的编码
        req.setCharacterEncoding("utf-8");
        // 获取到表单提交的参数
        String nickname = req.getParameter("nickname");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String id = req.getParameter("id");
        // 拼接insert语句
        String sql = "update userinfo set nickname='" + nickname + "',account='" + account + "'";
        if (!StringUtil.isEmpty(email)) {
            sql += ",email='" + email + "'";
        }
        if (!StringUtil.isEmpty(password)) {
            sql += ",password='" + password + "'";
        }
        sql += " where id=" + id;
        System.out.println(sql);
        PrintWriter out = StringUtil.getWriter(resp);// 获取输出对象
        try {
            // 执行插入语句获取受影响函数
            int count = DBTool.executeUpdate(sql);
            if (count == 0) {
                out.println("修改数据失败");
            } else {
                // 保存成功 跳转到列表也
                resp.sendRedirect("/cp7/user/list");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
